﻿using CQRS.Events;
using CQRS.Sample.Domain.BOs;
using CQRS.Sample.Domain.Commands;

namespace CQRS.Sample.Domain.CommandHandlers
{
    public class RegisterPlaneEmergencyCommandHandler : ICommandHandler<RegisterPlaneEmergencyCommand>
    {
        private readonly IDomainRepository<Event> _repository;

        public RegisterPlaneEmergencyCommandHandler(IDomainRepository<Event> repository)
        {
            _repository = repository;
        }

        #region ICommandHandler<RegisterPlaneEmergencyCommand> Members

        public void Execute(RegisterPlaneEmergencyCommand theEvent)
        {
            var plane = _repository.GetById<Plane>(theEvent.PlaneId);
            plane.EmergencyRegister(theEvent.PlaneId, theEvent.AirportName, theEvent.CrewCommander,
                                           theEvent.EmergencyDate, theEvent.FlightNumber, theEvent.WeatherConditions);
            _repository.Add(plane);
            _repository.Commit();
        }

        #endregion
    }
}